package com.example.common.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.web.reactive.function.client.WebClient;

import com.fasterxml.jackson.databind.ObjectMapper;

@Configuration
public class WebClientConfig {
	@Bean
	@LoadBalanced
	WebClient.Builder loadBalanced(ObjectMapper objectMapper) {
		return WebClient.builder().codecs(configurer -> {
			var jackson2JsonDecoder = new Jackson2JsonDecoder(objectMapper);

			jackson2JsonDecoder.setMaxInMemorySize(-1);

			configurer.defaultCodecs().jackson2JsonDecoder(jackson2JsonDecoder);
			configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper));
			configurer.defaultCodecs().maxInMemorySize(-1);
		});
	}
}
